



var Hogan = require('hogan.js');

var conf = {
    hostUrl: ''
}


var _util = {

    getServerUrl: function(urlPath) {
        return conf.hostUrl + urlPath;
    },

    request : function(param) {
        var _this = this;
        $.ajax({
            type: param.method || 'get',
            url : param.url || '',
            dataType: param.type || 'json',
            data: param.data || '',
            success: function(res) {
                console.log(res);
                if (0 === res.status) {
                    typeof param.success === 'function' && param.success(res.data);
                }else if(10 === res.status) {

                }
                 
            },
            error: function(error) {
                console.log(error);
                
            }
        });
    },

    doLogin: function () {
        window.location.href = './login.html?redirect=' + encodeURIComponent(window.location.href);
    },
    renderHTML: function(htmlTemplate, data) {
        var template = Hogan.compile(htmlTemplate);
        result = template.render(data);
        return result;

    },

    succussTip: function(msg) {
        alert(msg||'操作成功');
    },
    errorTip: function(msg) {
        alert(msg||'哪里不对了~');
    },

    validate: function(value, type) {
        var value = $.trim(value);
        if (type === 'require') {
            return !!value;
        }
        if (type === 'phone') {
            return /^1\d{10}$/.test(value);
        }
        if (type === 'email') {
            return /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/.test(value);
        }
    },
    goHome: function() {
        window.location.href = './index.html';
    }
};

module.exports = _util;